#!/bin/bash

# basic param and command line option to change it

MACHINE_TYPE=x86_64
# CFLAGS="-pipe -g"
CFLAGS="-pipe -O0"
MINGW_LIB="--enable-lib64 --disable-lib32"
MINGW_TRIPLE="x86_64-w64-mingw32"

if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
	echo "$0 [ 64d | 64r | 32d | 32r ]"
	exit 0
fi

if [ "$1" == "64r" ] || [ "$1" == "32r" ] ; then
	CFLAGS="-pipe -O2"
fi

if [ "$1" == "32r" ] || [ "$1" == "32d" ] ; then
	MACHINE_TYPE=i686
	MINGW_LIB="--enable-lib32 --disable-lib64"
	MINGW_TRIPLE="i686-w64-mingw32"
fi

export CFLAGS
export MINGW_LIB
export MINGW_TRIPLE

export M_ROOT=$(pwd)
export M_SOURCE=$M_ROOT/source
export M_BUILD=$M_ROOT/build
export M_CROSS=$M_ROOT/cross
export M_TARGET=$M_ROOT/target


# NOTE : must set both --host and --target, otherwise gcc build will failed
export BHT="--build=mipsel-unknown-linux-gnu --host=$MINGW_TRIPLE --target=$MINGW_TRIPLE"
# export BHT="--host=$MACHINE_TYPE-w64-mingw32"

export CC=$M_CROSS/bin/$MINGW_TRIPLE-gcc
export CXX=$M_CROSS/bin/$MINGW_TRIPLE-g++
export AR=$M_CROSS/bin/$MINGW_TRIPLE-ar
export RANLIB=$M_CROSS/bin/$MINGW_TRIPLE-ranlib
export AS=$M_CROSS/bin/$MINGW_TRIPLE-as
export LD=$M_CROSS/bin/$MINGW_TRIPLE-ld
export STRIP=$M_CROSS/bin/$MINGW_TRIPLE-strip
export NM=$M_CROSS/bin/$MINGW_TRIPLE-nm
export DLLTOOL=$M_CROSS/bin/$MINGW_TRIPLE-dlltool
export WINDRES=$M_CROSS/bin/$MINGW_TRIPLE-windres

# export CFLAGS=" -g -pipe -fno-leading-underscore "

export CXXFLAGS=$CFLAGS

export PATH=$M_CROSS/bin:$PATH

# export MAKE_OPT="-j 2"

set -x


# <2-2> build script
date

rm -rf $M_TARGET

mkdir -p $M_BUILD
cd $M_BUILD ; rm -rf tg_bin tg_gcc tg_gdb tg_m64 tg_m64_head


date
mkdir tg_m64_head
cd tg_m64_head
$M_SOURCE/mingw-w64/mingw-w64-headers/configure --build=mipsel-unknown-linux-gnu --host=$MINGW_TRIPLE \
  --prefix=$M_TARGET  --enable-sdk=directx
	# --enable-sdk=all   (ddk, directx)
make $MAKE_OPT
make install
cd ..

( cd $M_TARGET ; ln -s $MINGW_TRIPLE mingw ; cd $M_BUILD )


mkdir tg_bin
cd tg_bin

if [ "1" == "0" ] ; then
mkdir bfd
cat > bfd/config.cache <<EOF
ac_cv_have_decl_fseeko64=no
ac_cv_have_decl_ftello64=no
ac_cv_func_fseeko64=no
ac_cv_func_ftello64=no
EOF
fi

$M_SOURCE/binutils/configure $BHT --disable-nls \
  --disable-werror \
  --prefix=$M_TARGET --with-sysroot=$M_TARGET
# --with-sysroot=$M_TARGET
# --with-build-sysroot=$M_CROSS
# --enable-64-bit-bfd 
# --disable-multilib 
# make configure-host
make $MAKE_OPT
make install
cd ..


date
mkdir tg_m64
cd tg_m64
$M_SOURCE/mingw-w64/mingw-w64-crt/configure --build=mipsel-unknown-linux-gnu --host=$MINGW_TRIPLE \
  --prefix=$M_TARGET $MINGW_LIB
make $MAKE_OPT
make install
cd ..



# disable begin
if [ "1" == "1" ] ; then

rm -rf tg_gmp tg_mpfr tg_mpc $M_BUILD/for_target

date
mkdir tg_gmp
cd tg_gmp
# ( add limb_64=longlong for mingw arch, line about 3679(old3644) )
$M_SOURCE/gmp-5.0.0/configure $BHT --prefix=$M_BUILD/for_target --enable-static --disable-shared
make $MAKE_OPT
make install
cd ..


date
mkdir tg_mpfr
cd tg_mpfr
$M_SOURCE/mpfr-2.4.2/configure $BHT --prefix=$M_BUILD/for_target  --with-gmp=$M_BUILD/for_target --enable-static --disable-shared
make $MAKE_OPT
make install
cd ..

date
mkdir tg_mpc
cd tg_mpc
$M_SOURCE/mpc-0.8.1/configure $BHT --prefix=$M_BUILD/for_target  --with-gmp=$M_BUILD/for_target --enable-static --disable-shared
make $MAKE_OPT
make install
cd ..

fi
# disable end

date 
mkdir tg_gcc
cd tg_gcc
$M_SOURCE/gcc/configure $BHT --disable-nls \
  --enable-languages=c,c++ \
  --with-gmp=$M_BUILD/for_target \
  --enable-twoprocess \
  --disable-libstdcxx-pch \
  --prefix=$M_TARGET --with-sysroot=$M_TARGET
# --disable-multilib 
# --with-local-prefix=$M_TARGET
# --enable-twoprocess 
# --with-build-sysroot=$M_CROSS
# --with-sysroot=$M_TARGET
# --disable-libstdcxx-pch --enable-long-long 
make $MAKE_OPT
# make AS_FOR_TARGET=${AS} LD_FOR_TARGET=${LD}
make install
cd ..

date
mkdir tg_gdb
cd tg_gdb
$M_SOURCE/gdb/configure $BHT --disable-nls \
	--with-gmp=$M_BUILD/for_target \
	--disable-werror \
	--prefix=$M_TARGET
make $MAKE_OPT
make install
cd ..

# <2-3> end
cd $M_ROOT
date

